package com.eg366.proxy.spring;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.eg366.proxy.service.IBehavior;
import com.eg366.proxy.service.impl.StrangeBehavior;

public class Client {

	public static void main(String[] args) {
		/* 默认xml配置方式 */
//		defalutXmlType();
		/* aop标签配置方式 */
//		aopTagType();
		/* aop注解方式实现 */
		aopAnnotationType();
	}
	
	public static void defalutXmlType() {
		Resource resource = new ClassPathResource("proxy.xml");
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
		reader.loadBeanDefinitions(resource);
		IBehavior b = (IBehavior) factory.getBean("defaultBehavior");
		b.eat();
		
//		StrangeBehavior b2 = (StrangeBehavior) factory.getBean("strangeBehavior");
//		b2.eat();
	}
	
	public static void aopTagType() {
		/* 不能使用defalutXmlType中的加载xml的方式，使用该种方式无法实现aop的管理 */
		ApplicationContext context = new ClassPathXmlApplicationContext("proxyByAopTag.xml");
		
		IBehavior b = (IBehavior) context.getBean("defaultBehavior");
		b.eat();
		
		StrangeBehavior sb = (StrangeBehavior) context.getBean("strangeBehavior");
		sb.eat();
	}
	
	public static void aopAnnotationType() {
		/* 不能使用defalutXmlType中的加载xml的方式，使用该种方式无法实现aop的管理 */
		ApplicationContext context = new ClassPathXmlApplicationContext("proxyByAopAnnotation.xml");
		
		IBehavior b = (IBehavior) context.getBean("defaultBehavior");
		b.eat();
		
		StrangeBehavior sb = (StrangeBehavior) context.getBean("strangeBehavior");
		sb.eat();
	}
}
